import {Component, OnInit} from '@angular/core';
import {TipService} from "../../services/tip.service";
import {ExtendHttpService} from "../../../common/extend-http.service";
import {Router} from "@angular/router";
import {SocketService} from "../../services/socket.service";
@Component({
  selector: 'app-set-default',
  templateUrl: './set-default.component.html',
  styleUrls: ['./set-default.component.css']
})
export class SetDefaultComponent implements OnInit {
  isLoading = false;
  tipType = 'error';
  mname: string;

  constructor(private tipService: TipService,
              private extendHttp: ExtendHttpService,
              private router: Router,
              private socketService: SocketService) {
  }

  ngOnInit() {

    this.extendHttp.post('/merchantApp/basics/loginInfo/getInfo', {}).subscribe(response => {
      let res = response.json();
      if (res['code'] === '0') {
        this.mname = res['merchant']['mname'];
      } else if (res.code == '-2001') {
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
      }
    }, error => {
      this.tipService.change(true, '系统繁忙，操作失败！');
    });

    cBack = () => {
      $('#btn-back').trigger('click');
    }
  }

  submitData() {
    if (!this.isLoading) {
      this.isLoading = !this.isLoading;
      this.extendHttp.get('/merchantApp/login/exit', {}).subscribe(response => {
        let data = response.json();
        if (data.code == '0') {
          this.socketService.wsClose();
          this.router.navigate(['/login']);
        } else if (data.code == '-2001') {
          this.tipType = 'error';
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.tipType = 'error';
          this.tipService.change(true, data.message);
          this.isLoading = !this.isLoading;
        }
      }, error => {
        this.tipType = 'error';
        this.tipService.change(true, '系统繁忙，操作失败！');
        this.isLoading = !this.isLoading;
      });
    }
  }
}
